<?php
require_once 'BaseController.php';

/**
 * 【SP版】物件検索(地域・沿線・駅選択) コントローラ
 */
class Mobileapi_AreaController extends Mobileapi_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $this->doAction();
    }

    /**
     * メインアクション(post)
     */
    public function postAction()
    {
        $this->doAction();
    }

    private function doAction()
    {
        $params = $this->getParameters();
        $this->_data['Mapping_params'] = $params;

        $cat = $params['Category'];
        if (empty($cat)) $cat = 'index';

        $area = $params['Area'];

        if ($cat == 'index')
        {
            $this->makeTemplateParams1($area);
        }
        else if ($cat == 'line')
        {
            $rail = $params['RailCd'];
            if ($rail == '')
            {
                $cat = 'route';
                $this->makeTemplateParams2($area);
            }
            else
            {
                $this->makeTemplateParams3($area, $rail);
            }
        }

        $this->displayBody2($cat);
    }

    private function makeTemplateParams1($area)
    {
        $model = new Search_Model_Selsikucyoson();

        $area_code = $this->getPrefCode($area);
        $this->getPrefData($area_code);

        $params = array();
        $params['PrefCd'][] = $area_code;
        $data = array();
        $data['selcity'] = $model->getShikucyosonData($params);

        $model->convertShikucyosonData($data, '1');

        $this->_data['Mapping'] = $data['Mapping'];
        $this->_data['Mapping_area'] = $area;
        $this->_data['Mapping_area_code'] = $area_code;

        $data = array();
        Apm_Seo::generatePopEkiBlock($data, $params, array(), 5, 1);
        $this->_data['CrossEki'] = $data['CrossEki'];

        $data = array();
        Apm_Seo::generateKodawariBlock($data, $params, array(), 1);
        $this->_data['CrossKodawari'] = $data['CrossKodawari'];

        $data = array();
        Apm_Seo::generateTypeBlock($data, $params, array(), 1);
        $this->_data['CrossType'] = $data['CrossType'];

        $data = array();
        Apm_Seo::generatePopEnsenBlock($data, $params, array(), 5, 1);
        $this->_data['CrossEnsen'] = $data['CrossEnsen'];
    }

    private function makeTemplateParams2($area)
    {
        $area_code = $this->getPrefCode($area);
        $this->getPrefData($area_code);

        $model = new Search_Model_Selensen();
        $params = array();
        $params['PrefCd'][] = $area_code;

        $data = array();
        $data["selensen"] = $model->getEnsenDataByMulti($params);
        $model->convertEnsenDataByMulti($data);

        $this->_data['Mapping'] = $data["Mapping"];
        $this->_data['Mapping_area'] = $area;

        $data = array();
        Apm_Seo::generatePopEkiBlock($data, $params, array(), 5, 1);
        $this->_data['CrossEki'] = $data['CrossEki'];

        // 簡単検索に保存されているクッキーを表示される
        $simpleEki = $_COOKIE['simpleEki'];
        if (isset($simpleEki) && !empty($simpleEki) && $simpleEki !== '')
        {
            $vals = explode('|', $simpleEki);
            $eki = $vals[0];

            $vals2 = explode('-', $vals[1]);
            $pref = $vals2[0];

            if ($pref == $area_code)
            {
                $this->_data['throughEki'] = $eki;

                // 路線
                $temp = explode('.', $vals2[1]);
                $rel = array();
                foreach ($temp as $s)
                {
                    $rel[] = substr($s, 0, 4);
                }
                $ensens = implode(',', $rel);
                $this->_data['throughEnsen'] = $model->getRailDataByRailCd($pref, $ensens);
            }
        }
    }

    private function makeTemplateParams3($area, $rail)
    {
        $area_code = $this->getPrefCode($area);
        $this->getPrefData($area_code);

        $model = new Search_Model_Seleki();
        $params = array();
        $params['PrefCd'][] = $area_code;
        $params['RailCd'][] = $rail;

        $data = array();
        $data["seleki"] = $model->getEkiData($params);
        $model->convertEkiData($data);

        $this->_data['hoge'] = __var_dump($data);
        $this->_data['Mapping'] = $data["Mapping"];

        $eki = $data["seleki"][0];
        $this->_data['Mapping_area'] = $area;
        $this->_data['Mapping_ensen_code'] = $eki['ENSEN_CD'];
        $this->_data['Mapping_ensen_name'] = $eki['ENSEN_NAME'];

        // スタート～エンド駅取得
        $startEki = $eki['EKI_NAME']; // スタート駅
        $endEki = $data["seleki"][sizeof($data["seleki"]) - 1]['EKI_NAME']; // エンド駅
        $this->_data['START_END_EKI'] = '（' . $startEki . '～' . $endEki . '）';
        // スタート～エンド駅取得
        // ★★★乗り換えるデータを取得
        $transfer = $data['TRANSFER_CD'];
        // ★★★乗り換えるデータを取得

        $data = array();
        Apm_Seo::generateEnsenAreaBlock($data, $params, 'station_2', array());
        $this->_data['EnsenArea'] = $data['EnsenArea'];

        $data = array();
        Apm_Seo::generateOtherEnsenBlock($data, $params, 'station_2', array());
        $this->_data['OtherEnsen'] = $data['OtherEnsen'];

        // ★★★乗り換えるデータを取得
        if (isset($transfer) && $transfer != '')
        {
            $ensenmodel = new Search_Model_Selensen();
            $ensenparams = array();
            // $ensenparams ['PrefCd'] [] = $area_code;
            $ensenparams['transferCd'] = $transfer;
            $ensenparams['ensenCd'] = $eki['ENSEN_CD'];

            $ensendata = array();
            $ensendata["selensen"] = $ensenmodel->getEnsenDataByMulti($ensenparams);
            $ensenmodel->convertEnsenDataByMulti($ensendata);
            $this->_data['transfer'] = $ensendata['Mapping'];
        }
        // ★★★乗り換えるデータを取得
    }

    public function getPrefData($code)
    {
        $model = new Search_Model_Selareasearch();
        $this->_data['PrefInfo'] = $model->getSimpleArea($code);
        $this->_data['PrefName'] = $this->getPrefName($code);
        $this->_data['PrefNameJP'] = $this->getPrefNameJP($code);
        $this->_data['PrefNameJPWithoutPostfix'] = $this->getPrefNameWithoutPostfix($this->_data['PrefNameJP']);
    }

}
?>
